/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.ui;

import com.bytedance.scene.Scene;
import com.bytedance.scene.group.GroupScene;
import com.bytedance.scene.group.UserVisibleHintGroupScene;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.PageSlider;
import ohos.agp.components.PageSliderProvider;
import ohos.agp.components.StackLayout;

import java.util.List;

/**
 * Created by JiangQi on 7/30/18.
 */
public  class ScenePageAdapter extends PageSliderProvider {
    private GroupScene mGroupScene;
    private  List<UserVisibleHintGroupScene> fragments;
    private UserVisibleHintGroupScene mCurrentScene;
    private List<String> ll;
    public ScenePageAdapter(GroupScene scene, List<UserVisibleHintGroupScene> fragments, List<String> ll) {
        this.mGroupScene = scene;
        this.fragments = fragments;
        this.ll = ll;
    }
    @Override
    public int getCount() {
        return fragments.size();
    }
    @Override
    public String getPageTitle(int position) {
        return ll.get(position);
    }

    public UserVisibleHintGroupScene getItem(int position) {
        UserVisibleHintGroupScene scene = fragments.get(position);
        if (scene != mCurrentScene) {
            if (mCurrentScene != null) {
                mCurrentScene.setUserVisibleHint(false);
            }
            if (scene != null) {
                scene.setUserVisibleHint(true);
            }
            mCurrentScene = scene;
        }
        return fragments.get(position);
    }

    @Override
    public int getPageIndex(Object object) {
        UserVisibleHintGroupScene scene = (UserVisibleHintGroupScene) object;
        if (scene != mCurrentScene) {
            if (mCurrentScene != null) {
                mCurrentScene.setUserVisibleHint(false);
            }
            if (scene != null) {
                scene.setUserVisibleHint(true);
            }
            mCurrentScene = scene;
        }
        return super.getPageIndex(object);
    }



    //todo 就是这里
    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int position) {
        ComponentContainer.LayoutConfig config = new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,ComponentContainer.LayoutConfig.MATCH_PARENT);
        componentContainer.setLayoutConfig(config);
//        DirectionalLayout directionalLayout = (DirectionalLayout) componentContainer.getComponentParent();
        long itemId = getItemId(position);
        String name = makeFragmentName(itemId);
        PageSlider viewPager = (PageSlider) componentContainer.getComponentParent().getComponentParent();
//        ComponentContainer componentContainer1 = (ComponentContainer) componentContainer.getComponentParent();
//        int id2=componentContainer.getId();
        int id = viewPager.getId();
        UserVisibleHintGroupScene scene = mGroupScene.findSceneByTag(name);
        if (scene != null) {
            // setUserVisibleHint() must be invoked first, as the GroupScene.show() is directly executed.
            configSceneUserVisibleHint(viewPager, scene, position);
            mGroupScene.show(scene);
        } else {
            scene = getItem(position);
            configSceneUserVisibleHint(viewPager, scene, position);
            mGroupScene.add(id, scene, name);
        }
        return fragments.get(position);
    }
    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int itemId, Object object) {
        Scene scene = (Scene) object;
        mGroupScene.remove(scene);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return false;
    }

    private static String makeFragmentName(long id) {
        return "ohos:switcher:" + id;
    }
    public long getItemId(int position) {
        return position;
    }

    private void configSceneUserVisibleHint(PageSlider viewPager, UserVisibleHintGroupScene scene, int position) {
        if (mCurrentScene == null) {
            int currentItem = viewPager.getCurrentPage();
            if (currentItem == position) {
                mCurrentScene = scene;
            }
        }
        boolean visible = scene == mCurrentScene;
        if (visible) {
            if (scene.getUserVisibleHint()) {
                //to ensure that the behavior will be consistent with FragmentPagerAdapter
                scene.setUserVisibleHint(false);
            }
            scene.setUserVisibleHint(true);
        } else {
            if (scene.getUserVisibleHint()) {
                scene.setUserVisibleHint(false);
            }
        }
    }
}
